package com.nextwist.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDAO {
	Connection conn = null;
	  String url = "jdbc:mysql://localhost:3306/";
	  String dbName = "nexttwist";
	  String driver = "com.mysql.jdbc.Driver";
	  String userName = "root"; 
	  String password = "";
	  
	  /**
	   * This base constrator which will create the connection string
	   * 
	   */
	  public BaseDAO(){
		  try {
			  Class.forName(driver).newInstance();
			  conn = DriverManager.getConnection(url+dbName,userName,password);
			  System.out.println("Connected to the database");
			 /* conn.close();
			  System.out.println("Disconnected from database");*/
			  } catch (Exception e) {
			  e.printStackTrace();
			  }  
	  }
	  
	  /**
	   * This method is used to fetch the data from the database 
	   * @param queryString
	   * @return Resultset
	   */
	  public ResultSet getData(String queryString){
		  try{
			  PreparedStatement preparestatement = conn.prepareStatement(queryString);
			  ResultSet rs = preparestatement.executeQuery();
			  return rs;
		  } catch (Exception e) {
			  System.out.println(e.getMessage());
			  return null;
		  }	  
	  }
	  
	  /**
	   * This method is used to insert or update row
	   * @param queryString
	   * @return boolean
	   */
	  public boolean createData (PreparedStatement queryString){
		  try{
			  queryString.executeUpdate();
			  return true;
		  } catch (Exception e) {
			  System.out.println(e.getMessage());
			  return false;
		  }
	  }

}
